import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '../auth/auth.config';

export async function POST(request: Request) {
  try {
    const session = await getServerSession(authOptions);
    console.log('Session in activities API:', session);

    if (!session?.user?.id) {
      return NextResponse.json({ error: '请先登录' }, { status: 401 });
    }

    const body = await request.json();
    console.log('Request body:', body);
    const { title, date, description, tripId } = body;

    // 验证行程所有权
    const trip = await prisma.trip.findUnique({
      where: { id: tripId },
      select: { userId: true }
    });

    if (!trip || trip.userId !== session.user.id) {
      return NextResponse.json({ error: '无权操作此行程' }, { status: 401 });
    }

    const activity = await prisma.activity.create({
      data: {
        title,
        date: new Date(date),
        description,
        tripId,
      },
    });

    console.log('Created activity:', activity);
    return NextResponse.json(activity);
  } catch (error) {
    console.error('Error creating activity:', error);
    return NextResponse.json(
      { error: '创建活动失败' },
      { status: 500 }
    );
  }
} 